/**
 * SSI框架 <br> 
 * Copyright © 2010 HopeRun Inc. All Rights Reserved.
 */
package com.bj.jiufu.base.dao;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.dao.support.DaoSupport;
import org.springframework.util.Assert;

import com.alibaba.dubbo.container.page.Page;

/**
 * mybatis的dao基类，继承了BaseDao
 * 
 * @author wang_zhongpei
 * @function 框架程序
 * @param <E>
 *            需要操作的实体类类名
 * @param <PK>
 *            实体类对应的主键类型
 */
// xu_kai 2013-09-01 start
public abstract class AbstractDAO<E, PK extends Serializable> extends
        DaoSupport implements IBaseDAO<E, PK> {

    // xu_kai 2013-09-01 end
    /** 封装好的Session工厂对象 */
    @Resource
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 断言sqlSessionFactory不为空
     */
    protected void checkDaoConfig() {
        Assert.notNull(sqlSessionFactory, "sqlSessionFactory must be not null");
    }

    /**
     * 插入或更新操作的空实现，请在子类根据实际业务情况覆盖
     * 
     * @param entity
     *            待插入或者更新的对象
     */
    public void saveOrUpdate(E entity) {
    }

    /**
     * 获得session工厂对象
     * 
     * @return session工厂对象
     * 
     */
    public SqlSessionFactory getSqlSessionFactory() {
        return sqlSessionFactory;
    }

    /**
     * 根据session工厂对象创建 session模板
     * 
     * @param factory
     *            session工厂对象
     * 
     */
    public void setSqlSessionFactory(SqlSessionFactory factory) {
        this.sqlSessionFactory = factory;
    }


    /**
     * 根据ID查找记录
     * 
     * @param primaryKey
     *            条件ID
     * 
     * @return 找到的记录
     * 
     */
    public Object getById(String mapping ,PK primaryKey) {
        Object  object = sqlSessionFactory.openSession().selectOne(
        		mapping, primaryKey);
        return object;
    }

}